<?php
ob_start();
include('common/db.php');
include('common/xta.php');
include('common/xpath.php');

$xta = new xta();
$xta->load('stocks');
$all_stocks = $xta->data['stocks'];
print_r($all_stocks);

foreach($all_stocks as  $each_row) {
	$each_row['bsc'] = $each_row['bsc_code'];
	$each_row['date_added'] = date('Y-m-d H:i:s');
	$each_row['date_modified'] = $each_row['date_added'];
//	$db->add('stocks', $each_row);
}

print_r($db->all('stocks'));

die;
$xta = new xta();

function getList() {
	$xp = new xp();
	$xta = new xta();
	
	$all_companies = $xp->getData('company-list', 'http://money.rediff.com/gainers/bse/monthly');
	$limit = 10;
	$data = array();
	foreach($all_companies as  $each_company) {
		
		if(isset($limit) && ($limit == $counter)) break; else $counter++;
		
		$company_info = $xp->getData('company-info', $each_company['url']);
		
		$data[] = $company_info;		
	}
	
	$xta->data = $data;
	$xta->filename = 'stocks';
	$xta->saveAs('stocks', 'bsc_code');	
}

function getDayMoves() {
	$xp = new xp();
	$xta = new xta();
	
	$xta->load('stocks');
	$all_stocks = $xta->data['stocks'];
		
	foreach($all_stocks as $each_stock) {
		
		if(isset($limit) && ($limit == $counter)) break; else $counter++;

		$company_data = $xp->getData('stock-move-day', $each_stock['bsc_code']);
		$xta->data = $company_data;
		$xta->filename = $each_stock['bsc_code'];
		$xta->save('days', 'day');
	}
}

function getMonthMoves() {
	$xp = new xp();
	$xta = new xta();
	$xta->load('stocks');
	$all_stocks = $xta->data['stocks'];
	
	foreach($all_stocks as $each_stock) {
		
		if(isset($limit) && ($limit == $counter)) break; else $counter++;
 		
		$company_data = $xp->getData('stock-move-month', $each_stock['bsc_code']);
		$xta->data = $company_data;
//		echo $each_stock['bsc_code'];
//		print_r($company_data);
//		die;
		$xta->filename = $each_stock['bsc_code']."_monthly";
		$xta->save('months', 'month');
	}
}

//getList();
//getDayMoves();
//getMonthMoves();


$xta->load('stocks');
$all_stocks = $xta->data['stocks'];
//print_r($all_stocks);
//die;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery.tablesorter/themes/blue/style.css"></link>
<script type="text/javascript" src="jquery.tablesorter/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.tablesorter/jquery.tablesorter.js"></script>
<script type="text/javascript" src="jquery.sparkline/jquery.sparkline.js"></script>
<script type="text/javascript">
	$(function() {
		$("#companies").tablesorter();
	});
</script>
<body>
<table id="companies" cellspacing="0" class="tablesorter">
	<thead>
		<tr>
			<th>Code</th>
			<th>Name</th>
			<th>Rank</th>
			<th>Price</th>
			<th>Moves</th>
			<th>View</th>			
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>Code</th>
			<th>Name</th>
			<th>Rank</th>
			<th>Price</th>
			<th>Moves</th>
			<th>View</th>
		</tr>
	</tfoot>
	<tbody>
	<?php foreach($all_stocks as $each_stock) { ?>
	<tr>
		<td><?=$each_stock['bsc_code']?></td>
		<td><?=$each_stock['name']?></td>
		<td><?=$each_stock['rank']?></td>
		<td><?=$each_stock['price']?></td>
		<td><span id="sparkline_<?=$each_stock['bsc_code']?>"></span> <span style="float:right" id="day_<?=$each_stock['bsc_code']?>"></span></td>
		<td><a href="moves.php?code=<?=$each_stock['bsc_code']?>" target="_blank">D</a> | <a href="progress.php?code=<?=$each_stock['bsc_code']?>" target="_blank">M</a></td>	
	</tr>
	<?php (empty($all_stocks_code)) ? $all_stocks_code = $each_stock['bsc_code']: $all_stocks_code .= "," . $each_stock['bsc_code']; ?>
	<?php } ?>
	</tbody>
</table>
<script type="text/javascript">
	$(function() {
		var stocks = [<?=$all_stocks_code?>]; 
		$.each(stocks, function(index, value) {
			$.ajax({				
				url: 'progress.php?code='+value+'&ajax=1',
				type: 'get',
				success : function(data) {
					$("#sparkline_"+value).html(data);
					$("#sparkline_"+value).sparkline();
				}
			});  
		});
		
		$.each(stocks, function(index, value) {
			$.ajax({				
				url: 'moves.php?code='+value+'&ajax=1',
				type: 'get',
				success : function(data) {
					$("#day_"+value).html(data);
					$("#day_"+value).sparkline();
				}
			});  
		});
	});
</script>  
</body>
</html>